new Vue({
  el: '#app',
  data() {
    return {
      rules: {

      },
      StyleName: '',
      form: {
        index: {//首页
          Content: {//总背景
            backgroundColor: '',//背景颜色
            backgroundImage: '',//背景图片
            backgroundSize: '',//尺寸
            backgroundRepeat: '',//是否重复渲染
            Logo: false,//logo展示
            pic: '',
          },
          TopTabs:{
						backgroundColor:'',
					},
          Icon: {//图标
            textColor: '',//字体颜色
            backgroundColor: '',//背景颜色
            IconBackgroundColor:'',//图标背景颜色
          },
          Tabs: {//导航栏
            backgroundColor: '',//背景颜色
            textColor: '',//字体颜色
            selectColor: '',//选中的字体颜色
            //区域
          },
          Detail: {//商品卡片
            backgroundColor: '',//背景颜色
            themeColor: '',//主体颜色
          },
        },
        activity: {
          Content: {
            //总背景
            backgroundColor: "#dc3545",
            backgroundImage: "/6c1e39e6b6bc4258537d8b6691d41e4.png", //背景图片
            backgroundSize: "100% auto", //尺寸
            backgroundRepeat: "no-repeat", //是否重复渲染
            Logo: true, //logo展示
          },
          title: {
            //主标题
            color: "#fff",
          },
          subtitle: {
            //副标题
            color: "#fff",
          },
          Heading: {
            //商品卡片标题
            color: "#fff",
          },
          recommend: {
            //滑动区域背景颜色
            backgroundColor: "#f5f5f5",
          },
          scroll_view_item_H: {
            //商品卡片颜色
            backgroundColor: "#fff",
            color: "#fff",
          },
          markdown: {
            //降价
            color: "#000",
          },
        },
      },
      Size: [{
        value: 'contain',
        label: '缩小图片来适应尺寸'
      }, {
        value: 'cover',
        label: '扩展图片来填满背景'
      }, {
        value: '100% auto',
        label: '宽度100%高度自适应'
      }],
      Repeat: [{
        value: 'repeat',
        label: '横向和纵向平铺'
      }, {
        value: 'repeat-x',
        label: '横向平铺'
      }, {
        value: 'repeat-y',
        label: '纵向平铺'
      }, {
        value: 'no-repeat',
        label: '不平铺'
      }],
      loading: false,
      dialogVisible: false,//图片放大展示
      dialogImageUrl: '',//图片地址
      activeName: 'index'//标签页名字
    }
  },
  mounted() {

  },
  methods: {
    handlePictureCardPreview(file) {//查看图片
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    handleRemove() {//删除
      this.form.index.Content.pic = ''
      this.form.index.Content.backgroundImage = ''
    },
    uploadImage(file) {
      const _this = this;
      new Compressor(file, {
        quality: 1,
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                if (Result.Status == "success") {
                  for (const key in _this.form) {
                    if (_this.activeName == key) {
                      _this.form[key].Content.pic = e.target.result
                      _this.form[key].Content.backgroundImage = Result.Picture_Url
                    }
                  }
                  _this.$message.success('上传成功')
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    submitForm(formName) {
      let _this = this
      _this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(_this.form)
          this.form.index.Content.pic = '';
          this.form.activity.Content.pic = '';
          _this.loading = true
          $.ajax({
            url: DomainName + '/getAddAction?token=' + $.cookie('token'),
            type: 'POST',
            data: {
              Data: JSON.stringify({
                enterprise_appid_style: [{
                  App_Style_SerialNumber: '',
                  App_Style_Name: _this.StyleName,
                  App_Style_JSON: JSON.stringify(_this.form),
                  Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                }]
              }),
            },
            success: function (res) {
              if (res.Result == "成功") {
                _this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                window.history.go(-1);
              } else {
                _this.$message.error(res.Result);
              }
            },
            error: function () {
              console.log(error)
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        }
        else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})